package com.qfedu.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.qfedu.bean.AjaxMessage;
import com.qfedu.bean.Customer;
import com.qfedu.bean.TableData;
import com.qfedu.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 李小毅
 * @Description
 * @Date 2022-06-15
 */

@RestController
@RequestMapping("/sys/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @PostMapping("/add")
    public AjaxMessage addCustomer(Customer customer){
        try {
            customerService.insertCustomer(customer);
            return new AjaxMessage(true,"添加成功");
        } catch (Exception e) {
            return new AjaxMessage(false,"添加失败");
        }
    }


    @GetMapping("/table")
    public String table(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "2") Integer limit){

        //创建返回对象
        TableData<Customer> tableData = new TableData<Customer>();
        //配置pagehelper 来查询数据
        PageInfo<Customer> pageInfo = customerService.getAllCustomerByPage(page, limit);
        //设置总条数
        tableData.setCount(pageInfo.getTotal());
        //设置数据
        tableData.setData(pageInfo.getList());

        String s = JSON.toJSONString(tableData);
        return s;
    }


    @PostMapping("/update")
    public AjaxMessage updateCustomer(Customer customer){
        try {
            customerService.updateCustomer(customer);
            return new AjaxMessage(true,"更新成功");
        } catch (Exception e) {
            return new AjaxMessage(false,"更新失败");
        }
    }


    @PostMapping("/del")
    public AjaxMessage deleteCustomer(long[] ids){
        if (ids != null && ids.length > 0){
            customerService.deleteCustomersByIds(ids);
            return new AjaxMessage(true,"删除成功");
        }
        return new AjaxMessage(false,"删除失败");
    }
}

























